//
//  CDRegisterStep.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/5/18.
//

import Combine
//{"clientId":"428a8310cd442757ae699df5d894f051","password":"qq123123","userType":"app_user","grantType":"email","userName":"wan19900@yeah.net"}
//{"password":"qqq111qqq","userType":"app_user","clientId":"428a8310cd442757ae699df5d894f051","grantType":"email","userName":"wan19900@yeah.net"}
struct CDRegisterStep: LoginStep {
    struct InputData {
        let userName: String
        let password: String
        let grantType: LoginGrantType
    }

    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.Register.Response, Error> {
        Future { promise in
            let api = AuthAPI.Register(body: .init(userName: input.userName, password: input.password, grantType: input.grantType))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                case .success(let response):
                    if let response = response.data
                    {
                        Logger.log(.info, message: "注册成功: \(response)",tag: .login)
                        promise(.success(response))
                    }
                    else{
                        promise(.failure(NSError(domain: "注册失败", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                    }
                case .failure(let error):
                    promise(.failure(error))
                }
            })
        }
        .eraseToAnyPublisher()
    }
}
